//
//  myglLineView.m
//  myfistopengl
//
//  Created by huiti123 on 2017/3/31.
//  Copyright © 2017年 wtw. All rights reserved.
//

#import "myglLineView.h"

#import <OpenGLES/ES2/gl.h>

#import "glutils.h"


static const char vertexShaderStr[] = GLES_STRING(
                                                  
                                                  uniform mat4 projection;
                                                  uniform mat4 modelView;
                                                  attribute vec4 vPosition;
                                                  
                                                  void main(void)
{
    //gl_Position = projection * modelView * vPosition;
    
    gl_Position = vPosition;
    gl_PointSize = 10.0;
}
                                                  
                                                  );

static const char fragmentShaderStr[] = GLES_STRING(
                                                    
                                                    precision mediump float;
                                                    
                                                    uniform  vec4   fragColor;
                                                    
                                                    void main()
{
    gl_FragColor = vec4(1,0.0,0.0,1.0);
    
    //gl_FragColor = fragColor;
}
                                                    );




@implementation myglLineView{
        GLuint _position;
}


-(void) setupGlParamenter:(GLuint)program{
    _position = glGetAttribLocation(program, "vPosition");

    
}

-(void) drawElements
{
    GLfloat vertices[]={
    0.0f,  0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
    0.5f,  -0.5f, 0.0f,
    
    0.8f,  -0.8f, 0.0f
    };
    
    const GLfloat vertices1[] = {
        -1, -1, 0,   //左下
        1,  -1, 0,   //右下
        -1, 1,  0,   //左上
        1,  1,  0 }; //右上
    
    
    GLubyte indices[] = {
        0,1,1,2,2,3,3,0,2,0
    };
    
    glVertexAttribPointer(_position, 3, GL_FLOAT, GL_FALSE ,0, vertices1);
    glEnableVertexAttribArray(_position);
    
    glDrawElements(GL_LINES, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);
    
}
-(GLuint) getProgram{
    GLuint  _program = [glutils loadProgram:[NSString stringWithUTF8String:vertexShaderStr] withFragmentShaderStr:[NSString stringWithUTF8String:fragmentShaderStr]];
    if(_program == 0){
        return 0;
    }
    return _program;
}




@end
